//
//  TFTimerView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/10.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFTimerView: TFtouchesMoved {

    @objc public let pause = UILabel()
    @objc public let timeLabel = UILabel()
    @objc public let close = UIButton()
    //总时长
    @objc public var total:NSInteger = 0
    @objc public let timeSuperView = TFTimeSuperView()
    let backgroundView = TFTimeSuperView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.clipsToBounds = false
        self.timeSuperView.layer.masksToBounds = true
        //设置裁剪
          // 圆角半径  能把一个下正文体,改成圆形
        self.timeSuperView.backgroundColor = UIColor(red: 19/255.0, green: 21/255.0, blue: 25/255.0, alpha: 0.9)
       
        self.addSubview(self.timeSuperView)
       
        self.backgroundColor = UIColor.clear
        

        self.pause.text = "暂停中"
        self.pause.textAlignment =  .center
        self.pause.clipsToBounds = true
        self.pause.font = UIFont.PingFang(size: 13)
        self.pause.backgroundColor = UIColor.clear
        self.pause.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        self.timeSuperView.addSubview(self.pause)

        self.timeLabel.numberOfLines = 0
        self.timeSuperView.addSubview(self.timeLabel)
        self.timeLabel.font = UIFont.PingFang(size: 25)
        self.timeLabel.text = "00:00"
        self.timeLabel.textAlignment =  .center
        self.timeLabel.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        self.close.setImage(UIImage.init(named: "HTLive.bundle/close"), for: .normal)
       
        self.close.isHidden = true;
        self.close.backgroundColor = UIColor(red: 49/255.0, green: 52/255.0, blue: 57/255.0, alpha: 0.9)
        self.close.addTarget(self, action: #selector(closeClick), for: .touchUpInside)
      
        self.addSubview(self.close)
    }
    @objc func closeClick()
    {
        self.endTimeLaer()
        self.isHidden = true
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.timeSuperView.frame = self.bounds
        self.timeSuperView.layer.cornerRadius =  self.frame.size.width/2
        self.timeLabel.frame = self.timeSuperView.bounds
        
        
        self.close.frame = CGRect(x: self.frame.size.width, y: self.timeSuperView.frame.origin.y-14, width: 28, height: 28)

        self.close.layer.cornerRadius = self.close.frame.size.width/2

        self.pause.frame = CGRect(x: 0, y: self.frame.size.height-18.8-21, width: self.frame.size.width, height: 18.8)
       
    }
   @objc func endTimeLaer()
    {
        self.timeSuperView.proportion = 0
        self.timeLabel.font = UIFont.PingFang(size: 20)
        self.timeLabel.text = "计时结束"
        self.close.isHidden = false
        self.timeSuperView.setNeedsDisplay()
      
    }

    
    
    /* 定时器开始倒计时
    *@param total 总时长
    * @param duration 剩余的时长倒计
    */
   @objc func timerStartWidthTotal(_ total:NSInteger,duration:NSInteger)
    {
        self.pause.isHidden = true
        self.close.isHidden = true
        self.StartWidthTotal(total, duration: duration)
      
    }
    //定时器暂停
    @objc func timerPause(_ duration:NSInteger)
    {
        self.pause.isHidden = false
        self.StartWidthTotal(self.total, duration: duration)
    }
    /* 定时器开始倒计时
    *@param total 总时长
    * @param duration 剩余的时长倒计
    */
    func StartWidthTotal(_ total:NSInteger,duration:NSInteger)

    {
        self.total = total;

        
        
         if (duration==0) {
             self.endTimeLaer()
           
         }else  if (duration>0) {
             
    
             //当前的
             let one:CGFloat =  CGFloat(duration)
             
             //总的
             
             let two:CGFloat = CGFloat(total)

         
            let text = TalkfunSharedTools.minutesSndSeconds(duration)
             
             
            self.timeSuperView.proportion = (one/two)
             
            self.timeLabel.font = UIFont.PingFang(size: 25)
             
             
             self.timeLabel.text = text as String

        
        }

        self.timeSuperView.setNeedsDisplay()
    }
    
    //更新语言
    @objc func upDataLanguage()

    {
        
        self.pause.text = "暂停中"
    }
    //超出点击
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
       
        var view = super.hitTest(point, with: event)
        if (view == nil) {
         
        let newPoint:CGPoint =  self.close.convert(point, from: self)
            // 判断触摸点是否在button上
            
            if (self.close.bounds.contains(newPoint)) {
                view = self.close
            }
        }
        return view
        
        
    }
   
  
}
